package com.example.shop_2210.login

import android.content.Intent
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2210.R
import com.example.shop_2210.databinding.ActivityLoginCodeBinding

/**
 * 路由路径命名习惯 /XXX/XXX
 */
@Route(path = "/app/LoginCodeActivity")
class LoginCodeActivity : BaseActivity<ActivityLoginCodeBinding,LoginViewModel>() {
    override fun initData() {

        viewModel.loginCodeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //登录成功 保存token 和手机号 方便后续修改密码
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)
                ARouter.getInstance().build("/app/ShopActivity").navigation()
            }
        }
        //监听发送验证码的返回值
        viewModel.sendCodeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }
        }
        binding.goLogin.setOnClickListener {
            val intent = Intent(this,LoginActivity::class.java)
            startActivity(intent)
            finish()//关闭当前界面
        }
    }

    override fun initView() {
        //实现验证码登录的请求
        binding.loginCode.setOnClickListener {
            val phone = binding.etPhone.text.toString()
            val code = binding.etCode.text.toString()
            if(phone.isEmpty()||code.isEmpty()){
                ToastUtils.showLong("用户名和验证码不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            viewModel.loginCode(binding.etCode.text.toString(),
                binding.etPhone.text.toString())
        }
        //点击发送验证码
        binding.btnCode.setOnClickListener {
            if(binding.etPhone.text.toString().isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(binding.etPhone.text.toString())){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }

            viewModel.sendCode(binding.etPhone.text.toString())
        }

        //富文本
        val spannableStringBuilder = SpannableStringBuilder(binding.cb.text.toString())
        spannableStringBuilder.setSpan(ForegroundColorSpan(Color.RED),6,11,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        val clickableSpan = object:ClickableSpan(){
            override fun onClick(widget: View) {
                /*ToastUtils.showLong("点击了用户权限")*/
               /* val intent = Intent(this@LoginCodeActivity,WebViewActivity::class.java)
                intent.putExtra("url","http://www.baidu.com")
                startActivity(intent)*/
                //带参数跳转
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.baidu.com").withInt("key",1).navigation()
            }
        }
        spannableStringBuilder.setSpan(clickableSpan,13,19,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.cb.movementMethod = LinkMovementMethod.getInstance()//点击起作用必须添加这行
        binding.cb.text = spannableStringBuilder


        //alertDialog弹出框
      /*  AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage("这是提示的内容")
            .setPositiveButton("确定",object:DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {
                    ToastUtils.showLong("点击了确定")
                }
            })
            .setNegativeButton(
                "取消"
            ) { dialog, which ->
                finish()
            }
            .show()*/


    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout() = R.layout.activity_login_code

}